package operativa.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import operativa.action.utils.UbicacionRecorrido;
import operativa.bean.entity.Location;
import operativa.model.dao.LocationDAO;
import operativa.utils.Constantes.LocationType;
import operativa.utils.Constantes.LocationType;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.config.entities.Parameterizable;

public class DistributionAction extends ActionSupport implements Preparable, Parameterizable{

	private static final long serialVersionUID = 1L;
	private List<Location> harbors;
	private List<Location> fishShops;
	private LocationDAO locationDAO = new LocationDAO();
	private Map<String, UbicacionRecorrido> harborMap = new HashMap<String, UbicacionRecorrido>();
	private Map<String, UbicacionRecorrido> fishShopMap = new HashMap<String, UbicacionRecorrido>();
	private List<Location> harborList;
	private List<Location> fishShopsList;
	private String selHarbors;
	private String selFishShops;
	private Map<String, Object> params;
	
	@Override
	public void prepare() throws Exception {
		harbors = locationDAO.getByTipoUbicacion(LocationType.HARBOR.toString());
		fishShops = locationDAO.getByTipoUbicacion(LocationType.FISHSHOP.toString());
		for (Location f : harbors) {
			harborMap.put(f.getId().toString(), new UbicacionRecorrido(f));
		}
		for (Location f : fishShops) {
			fishShopMap.put(f.getId().toString(), new UbicacionRecorrido(f));
		}
	}
	
	public String init(){
		return SUCCESS;
	}
	
	public List<UbicacionRecorrido> getTravelList(List<Location> ubicaciones){
		List<UbicacionRecorrido> result = new ArrayList<UbicacionRecorrido>();
		for (Location u : ubicaciones){
			result.add(new UbicacionRecorrido(u, "false"));
		}
		return result;
	}
	
	public String nextStep(){
		harborList = new ArrayList<Location>();
		fishShopsList = new ArrayList<Location>();
		for (String s : harborMap.keySet()) {
			if (harborMap.get(s).getChoosen().equals("true"))
				harborList.add(harborMap.get(s).getLocation());
		}
		for (String s : fishShopMap.keySet()) {
			if (fishShopMap.get(s).getChoosen().equals("true"))
				fishShopsList.add(fishShopMap.get(s).getLocation());
		}
		if (harborList.size() == 0 || fishShopsList.size() == 0){
			return "error";
		}
		return "nextStep";
	}

	public List<Location> getHarbors() {
		return harbors;
	}

	public void setHarbors(List<Location> fabricas) {
		this.harbors = fabricas;
	}

	public List<Location> getFishShops() {
		return fishShops;
	}

	public void setFishShops(List<Location> destinos) {
		this.fishShops = destinos;
	}

	public Map<String, UbicacionRecorrido> getHarborMap() {
		return harborMap;
	}

	public Map<String, UbicacionRecorrido> getFishShopMap() {
		return fishShopMap;
	}

	public void setHarborMap(Map<String, UbicacionRecorrido> fabMap) {
		this.harborMap = fabMap;
	}

	public void setFishShopMap(Map<String, UbicacionRecorrido> destMap) {
		this.fishShopMap = destMap;
	}
	
	public String getHarborListString(){
		String result = "";
		for (Location harbor : harborList) {
			result+=harbor.getId().toString() + ",";			
		}
		return result.substring(0, result.length()-1);
	}
	
	public String getFishShopsListString(){
		String result = "";
		for (Location destino : fishShopsList) {
			result+=destino.getId().toString() + ",";			
		}
		return result.substring(0, result.length()-1);
	}

	public String getSelHarbors() {
		return selHarbors;
	}

	public void setSelHarbors(String selHarbors) {
		this.selHarbors = selHarbors;
	}

	public String getSelFishShops() {
		return selFishShops;
	}

	public void setSelFishShops(String selFishShops) {
		this.selFishShops = selFishShops;
	}

	@Override
	public void addParam(String arg0, Object arg1) {
		this.params.put(arg0, arg1);
		
	}

	@Override
	public Map getParams() {
		return this.params;
	}

	@Override
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	
}
